package cn.nawang.ebeim.ui.util;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DirectoryUtil {

    public static File[] getSystemRoots() {
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File f = fsv.getHomeDirectory();
        File[] roots = File.listRoots();
        File[] fs = new File[roots.length + 1];
        fs[0] = f;
        for (int i = 1; i < roots.length + 1; i++) {
            fs[i] = roots[i - 1];
        }
        return fs;
    }

    public static File[] getDirs(String parent, boolean isRoot) {
        if (isRoot) {
            return getSystemRoots();
        }
        if (parent.substring(parent.length() - 1, parent.length()).equals(":")) {
            parent = parent + "\\";
        }

        File file = new File(parent);
        if (file.exists() && file.listFiles() != null) {
            List<File> fList = new ArrayList<File>();
            File[] fArr = file.listFiles();
            for (int i = 0; i < fArr.length; i++) {
                File f = fArr[i];
                if (!f.isHidden() /*&&f.isDirectory() && f.listFiles()!=null*/) {
                    fList.add(f);
                }
            }
            return fList.toArray(new File[fList.size()]);
        }

        return new File[0];
    }





}
